package Chapter8;
//新增程序

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class Drawing2DExample {
    JFrame frame;
    MyPanel panel;

    public static void main(String[] args) {
        Drawing2DExample d2e = new Drawing2DExample();
        d2e.go();
    }

    void go() {
        frame = new JFrame("Draw2D测试程序");
        panel = new MyPanel();
        panel.repaint();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.setSize(360, 280);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class MyPanel extends JPanel {
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        BasicStroke bs = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
        g2d.setStroke(bs);
        g2d.setColor(Color.CYAN);
        Line2D line1 = new Line2D.Double(30, 30, 240, 100);
        g2d.draw(line1);
        CubicCurve2D curve = new CubicCurve2D.Double(10, 150, 110, 260, 210, 80, 310, 160);
        g2d.setColor(Color.green);
        g2d.draw(curve);
        GradientPaint gp = new GradientPaint(150, 150, Color.RED, 280, 30, Color.BLUE);
        g2d.setPaint(gp);
        Line2D line2 = new Line2D.Double(150, 150, 280, 30);
        g2d.draw(line2);
        g2d.setColor(Color.orange);
        RoundRectangle2D circle = new RoundRectangle2D.Double(30, 40, 100, 100, 100, 100);
        g2d.draw(circle);
        AffineTransform at = AffineTransform.getScaleInstance(1.0, 1.5);
        g2d.setTransform(at);
        g2d.setColor(Color.BLUE);
        g2d.setStroke(new BasicStroke());
        g2d.draw(circle);
    }
}